package com.example.capsule_chat0322.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.ChatRecordBean;
import com.example.capsule_chat0322.interfaces.ChatRecordItemOnClickInterface;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/1 21:21
 */
public class ChatRecordAdapter extends RecyclerView.Adapter<ChatRecordAdapter.ViewHold> implements View.OnClickListener {

    private List<ChatRecordBean> chatRecordBeanList;
    private ChatRecordItemOnClickInterface chatRecordItemOnClickInterface;
    private RecyclerView rvParent;

    public void setFunctionItemOnClickInterface(ChatRecordItemOnClickInterface chatRecordItemOnClickInterface) {
        this.chatRecordItemOnClickInterface = chatRecordItemOnClickInterface;
    }

    static class ViewHold extends RecyclerView.ViewHolder {

        LinearLayout chatRecordItem;
        TextView chatRecord;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            chatRecordItem = itemView.findViewById(R.id.chat_record_item);
            chatRecord = itemView.findViewById(R.id.chat_record);
        }
    }


    public ChatRecordAdapter(List<ChatRecordBean> chatRecordBeanList) {
        this.chatRecordBeanList = chatRecordBeanList;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        rvParent = (RecyclerView) parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_record_item, parent, false);
        view.setOnClickListener(this);
        return new ChatRecordAdapter.ViewHold(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, int position) {

        ChatRecordBean chatRecordBean = chatRecordBeanList.get(position);
        holder.chatRecordItem.setVisibility(View.VISIBLE);
        holder.chatRecord.setText(chatRecordBean.toString());
    }

    @Override
    public int getItemCount() {
        return chatRecordBeanList.size();
    }


    @Override
    public void onClick(View view) {
        //根据RecyclerView获得当前View的位置
        int position = rvParent.getChildAdapterPosition(view);
        //程序执行到此，会去执行具体实现的onItemClick()方法
        if (chatRecordItemOnClickInterface != null) {
            chatRecordItemOnClickInterface.onItemClick(rvParent, view,
                    chatRecordBeanList.get(position).getRoomName(),
                    chatRecordBeanList.get(position).getSenderId(),
                    chatRecordBeanList.get(position).getSenderName(),
                    chatRecordBeanList.get(position).getContent(),
                    chatRecordBeanList.get(position).getSendTime());
        }
    }


}
